
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @author 帅小伙
 * @date 2022/1/3
 * @description         Direct 消费者
 */
public class Consumer5 {
    private static final String EXCHANGE_NAME = "direct_logs";

    public void consume(int... params) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        String queueName="";
        if (params[0] == 0) {
            queueName = "console";
            // 定义
            channel.queueDeclare(queueName,false,false,false,null);
            // 队列和交换机绑定
            channel.queueBind(queueName, EXCHANGE_NAME, "info");
        }else{
            queueName = "disk";
            // 定义
            channel.queueDeclare(queueName,false,false,false,null);
            // 队列和交换机绑定
            channel.queueBind(queueName, EXCHANGE_NAME, "error");
            channel.queueBind(queueName, EXCHANGE_NAME, "waring");
        }

        System.out.println(Thread.currentThread().getName() + "  消费者启动等待消费.........");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody());
            String routingKey = delivery.getEnvelope().getRoutingKey();
            System.out.println(Thread.currentThread().getName()+"接收到"+routingKey+"的消息" + message);
        };

        channel.basicConsume(queueName, true, deliverCallback, (consumerTag) -> {
            System.out.println(consumerTag + "消费者取消消费信息");
        });
    }
}
